package com.qf.dao.impl;

import com.qf.dao.TeacherDao;
import com.qf.pojo.TBteacher;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl  implements TeacherDao {
    @Override
    public List<TBteacher> findAll() {
        List list = new ArrayList();
        Connection connection = null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.创建数据库链接对象
             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xa_2106?characterEncoding=utf-8&serverTimezone=UTC&useUnicode=true&useSSL=false", "root", "59852369");
             preparedStatement = connection.prepareStatement("select * from tb_teacher");
             resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                TBteacher tBteacher = new TBteacher();
                tBteacher.setAge(resultSet.getInt("age"));
                tBteacher.setId(resultSet.getInt("id"));
                tBteacher.setTeacherName(resultSet.getString("teacher_name"));
                tBteacher.setSex(resultSet.getString("sex"));
                list.add(tBteacher);
            }
            return list;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public int del(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement =null;
        int i=0;
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.创建数据库链接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xa_2106?characterEncoding=utf-8&serverTimezone=UTC&useUnicode=true&useSSL=false", "root", "59852369");
            preparedStatement = connection.prepareStatement("delete  from tb_teacher where id = ?");
            preparedStatement.setInt(1,id);
            i = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            try {

                preparedStatement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return i;
    }
}
